import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from Python.test.Utils.DB_Utils import DBUtils


class TestDuxiaoman:
    db = DBUtils(
            host="localhost",
            user="root",
            passwd="root",
            database="finance",
        )

    def test_login(self):
        # 创建Edge浏览器对象
        driver = webdriver.Edge()
        # 窗口最大化
        driver.maximize_window()
        # 打开度小满后台管理系统
        driver.get('http://localhost:90/')
        time.sleep(1)
        # 登录 测试
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        assert driver.title=='后台管理系统','登录失败'


    def test_register(self):
        # 创建Edge浏览器对象
        driver = webdriver.Edge()
        # 窗口最大化
        driver.maximize_window()
        # 打开度小满后台管理系统
        driver.get('http://localhost:90/')
        time.sleep(1)
        # 注册
        driver.find_element(By.LINK_TEXT, '还没有账号？去注册').click()
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin131")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.NAME, 'repassword').send_keys("123456")
        time.sleep(1)
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        driver.quit()
        sql = 'select username from user where username = "admin131"'
        result = self.db.sql_d(sql)
        assert result[0][0]=='admin131'


    def test_add_users(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)

        # 用户信息管理-用户信息 测试
        driver.find_element(By.LINK_TEXT, '用户信息管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '用户信息').click()
        time.sleep(1)
        # 增
        driver.find_element(By.XPATH , '//*[@id="user_add_modal_btn"]').click()
        time.sleep(1)
        driver.find_element(By.ID, 'username_add_input').send_keys("12312")
        time.sleep(1)
        driver.find_element(By.ID, 'password_add_input').send_keys("123456")
        time.sleep(1)
        driver.find_element(By.ID, 'realname_add_input').send_keys("王骏楠")
        time.sleep(1)
        driver.find_element(By.ID, 'phone_add_input').send_keys("15147641675")
        time.sleep(1)
        driver.find_element(By.ID, 'email_add_input').send_keys("123@163.com")
        time.sleep(1)
        driver.find_element(By.ID, 'user_save_btn').click()
        time.sleep(1)
        sql = 'select username from user where username = "12312"'
        result = self.db.sql_d(sql)
        assert result[0][0] == '12312'

    def test_upd_users(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        # 用户信息管理-用户信息
        driver.find_element(By.LINK_TEXT, '用户信息管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '用户信息').click()
        time.sleep(1)
        # 改
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[1]/div/div[2]/div/table/tbody/tr[1]/td[6]/button[1]').click()
        time.sleep(1)
        driver.find_element(By.ID, 'realname_update_input').send_keys("一")
        time.sleep(1)
        driver.find_element(By.ID, 'user_update_btn').click()
        time.sleep(2)
        sql = 'select realname from user where username = "lisi"'
        result = self.db.sql_d(sql)
        assert result[0][0] == '李四一'


    def test_del_users(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        # 用户信息管理-用户信息 测试
        driver.find_element(By.LINK_TEXT, '用户信息管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '用户信息').click()
        time.sleep(1)
        # 删
        driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div[1]/div/div[2]/div/table/tbody/tr[1]/td[6]/button[2]').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(5)
        sql = 'select * from user where username = "lisi"'
        result = self.db.sql_d(sql)
        assert result == False, '删除失败，库中仍存在'


    def test_add_change(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        # 理财产品管理-零钱理财 测试
        driver.find_element(By.LINK_TEXT, '理财产品管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '零钱理财').click()
        time.sleep(1)
        # 增
        driver.find_element(By.XPATH, '//*[@id="add_modal_btn"]').click()
        time.sleep(1)
        driver.find_element(By.ID, 'name_add_input').send_keys("钱到家")
        time.sleep(1)
        driver.find_element(By.ID, 'annualincome_add_input').send_keys("30")
        time.sleep(1)
        driver.find_element(By.ID, 'peiincome_add_input').send_keys("0.01")
        time.sleep(1)
        driver.find_element(By.ID, 'invesmoney_add_input').send_keys("300")
        time.sleep(1)
        driver.find_element(By.ID, 'save_btn').click()
        time.sleep(1)


    def test_upd_change(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        # 理财产品管理-零钱理财 测试
        driver.find_element(By.LINK_TEXT, '理财产品管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '零钱理财').click()
        time.sleep(1)
        # 改
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[1]/div/div[2]/div/table/tbody/tr[1]/td[6]/button[1]').click()
        time.sleep(1)
        driver.find_element(By.ID, 'name_update_input').send_keys("通")
        time.sleep(1)
        driver.find_element(By.ID, 'update_btn').click()
        time.sleep(1)


    def test_del_change(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        # 理财产品管理-零钱理财 测试
        driver.find_element(By.LINK_TEXT, '理财产品管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '零钱理财').click()
        time.sleep(1)
        # 删
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[1]/div/div[2]/div/table/tbody/tr[1]/td[6]/button[2]').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(1)


    def test_permission(self):
        driver = webdriver.Edge()
        driver.maximize_window()
        driver.get('http://localhost:90/')
        time.sleep(1)
        driver.find_element(By.NAME, 'username').send_keys("admin")
        driver.find_element(By.NAME, 'password').send_keys("123456")
        driver.find_element(By.ID, 'login_btn').click()
        time.sleep(1)
        # 权限管理-管理员权限 测试
        driver.find_element(By.LINK_TEXT, '权限管理').click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, '管理员权限').click()
        time.sleep(1)
        # 变更
        driver.find_element(By.XPATH, "//span[contains(text(), '网贷管理')]").click()
        time.sleep(1)
        driver.find_element(By.ID, 'submit_modal_btn').click()
        time.sleep(2)
        assert driver.find_element(By.LINK_TEXT, '网贷管理'),'网贷管理不存在，变更失败'






